package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.DictDto;
import com.yc.sole.system.entity.Dict;
import com.yc.sole.system.mapper.DictMapper;
import com.yc.sole.system.request.DictQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class DictRepository extends ServiceImpl<DictMapper, Dict> {

    /**
     * 新增
     *
     * @param dictDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveDict(DictDto dictDto) {
        Dict entity = new Dict();
        BeanUtil.copyProperties(dictDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param dictDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDictById(DictDto dictDto) {
        Dict entity = new Dict();
        BeanUtil.copyProperties(dictDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDictByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public DictDto selectDictById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), DictDto.class);
    }

    /**
     * 根据key获取
     *
     * @param key
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public DictDto selectDictByKey(String key) {
        LambdaQueryWrapper<Dict> wrapper = Wrappers.<Dict>lambdaQuery().eq(Dict::getDictKey, key);
        return BeanUtil.copyProperties(this.getOne(wrapper), DictDto.class);
    }

    /**
     * 查询所有字典
     *
     * @return
     */
    public List<DictDto> selectAllDict() {
        LambdaQueryWrapper<Dict> wrapper = Wrappers.<Dict>lambdaQuery().eq(Dict::getState, CommonState.ENABLE);
        return BeanUtil.copyToList(this.list(wrapper), DictDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<DictDto> selectDictPage(Paging page, DictQueryReq dictQueryReq) {
        LambdaQueryWrapper<Dict> queryWrapper = new QueryWrapper<Dict>().lambda()
                .like(CharSequenceUtil.isNotBlank(dictQueryReq.getDictName()), Dict::getDictName, dictQueryReq.getDictName())
                .like(CharSequenceUtil.isNotBlank(dictQueryReq.getDictKey()), Dict::getDictKey, dictQueryReq.getDictKey())
                .eq(Objects.nonNull(dictQueryReq.getState()), Dict::getState, dictQueryReq.getState())
                .orderByDesc(Dict::getId);
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), DictDto.class);
    }
}
